<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="员工名字" prop="staffName">
      <el-input v-model="dataForm.staffName" placeholder="员工名字"></el-input>
    </el-form-item>
    <el-form-item label="员工年龄" prop="staffAge">
      <el-input v-model="dataForm.staffAge" placeholder="员工年龄"></el-input>
    </el-form-item>
    <el-form-item label="员工编号" prop="staffNo">
      <el-input v-model="dataForm.staffNo" placeholder="员工编号"></el-input>
    </el-form-item>
    <el-form-item label="员工性别" prop="staffSex">
			<el-select v-model="dataForm.staffSex">
				<el-option label="男" value="1"></el-option>
				<el-option label="女" value="2"></el-option>
			</el-select>
    </el-form-item>
    <el-form-item label="员工电话" prop="staffTel">
      <el-input v-model="dataForm.staffTel" placeholder="员工电话"></el-input>
    </el-form-item>
    <el-form-item label="员工生日" prop="staffBirthday">
		 <el-date-picker
		      v-model="dataForm.staffBirthday"
		      type="date"
		      placeholder="选择日期">
		    </el-date-picker>
    </el-form-item>
    <el-form-item label="身份证号" prop="staffIdCard">
      <el-input v-model="dataForm.staffIdCard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="最大免单" prop="maxFree">
      <el-input v-model="dataForm.maxFree" placeholder="最大免单(如200,300)"></el-input>
    </el-form-item>
    <el-form-item label="最大手动优惠" prop="maxManualOffer">
      <el-input v-model="dataForm.maxManualOffer" placeholder="最大手动优惠(如200,300)"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {isMobile} from "../../../../../utils/validate";

  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      };
      var validateAge = (rule, value, callback) => {
        if (isNaN(value)) {
          callback(new Error('格式错误'))
        } else {
          callback()
        }
      }
      return {
				disabledSubmit:false,
        visible: false,
        dataForm: {
          staffId: 0,
          staffName: '',
          userId: '',
          sellerId: '',
          staffAge: '',
          staffNo: '',
          staffSex: '',
          staffTel: '',
          staffBirthday: '',
          staffIdCard: '',
          entryTime: '',
          status: '',
          maxFree: '',
          maxManualOffer: '',
          createUserId: '',
          createTime: '',
          remark: ''
        },

        dataRule: {
          staffName: [
            { required: true, message: '员工名字不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '所属租户不能为空', trigger: 'blur' }
          ],
          staffAge: [
            { required: true, message: '员工年龄不能为空', trigger: 'blur' },
            { validator: validateAge, trigger: 'blur' }
          ],
          staffNo: [
            { required: true, message: '员工编号不能为空', trigger: 'blur' }
          ],
          staffSex: [
            { required: true, message: '员工性别不能为空', trigger: 'blur' }
          ],
          staffTel: [
            { required: true, message: '员工电话不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          staffBirthday: [
            { required: true, message: '员工生日不能为空', trigger: 'blur' }
          ],
          staffIdCard: [
            { required: true, message: '身份证号码不能为空', trigger: 'blur' },
            { validator: validateAge, trigger: 'blur' }
          ],
          entryTime: [
            { required: true, message: '入职时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（在职，离职，请假）不能为空', trigger: 'blur' }
          ],
          maxFree: [
            { validator: validateAge, trigger: 'blur' }
          ],
          maxManualOffer: [
            { validator: validateAge, trigger: 'blur' }
          ],
          // createUserId: [
          //   { required: true, message: '创建人不能为空', trigger: 'blur' }
          // ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // remark: [
          //   { required: true, message: '备注不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.staffId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.staffId) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorestaff/info/${this.dataForm.staffId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.staffName = data.tStoreStaff.staffName
                this.dataForm.userId = data.tStoreStaff.userId
                this.dataForm.sellerId = data.tStoreStaff.sellerId
                this.dataForm.staffAge = data.tStoreStaff.staffAge
                this.dataForm.staffNo = data.tStoreStaff.staffNo
                this.dataForm.staffSex = data.tStoreStaff.staffSex
                this.dataForm.staffTel = data.tStoreStaff.staffTel
                this.dataForm.staffBirthday = data.tStoreStaff.staffBirthday
                this.dataForm.staffIdCard = data.tStoreStaff.staffIdCard
                this.dataForm.entryTime = data.tStoreStaff.entryTime
                this.dataForm.status = data.tStoreStaff.status
                this.dataForm.maxFree = data.tStoreStaff.maxFree
                this.dataForm.maxManualOffer = data.tStoreStaff.maxManualOffer
                this.dataForm.createUserId = data.tStoreStaff.createUserId
                this.dataForm.createTime = data.tStoreStaff.createTime
                this.dataForm.remark = data.tStoreStaff.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorestaff/${!this.dataForm.staffId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'staffId': this.dataForm.staffId || undefined,
                'staffName': this.dataForm.staffName,
                'userId': this.dataForm.userId,
                'sellerId': this.dataForm.sellerId,
                'staffAge': this.dataForm.staffAge,
                'staffNo': this.dataForm.staffNo,
                'staffSex': this.dataForm.staffSex,
                'staffTel': this.dataForm.staffTel,
                'staffBirthday': this.dataForm.staffBirthday,
                'staffIdCard': this.dataForm.staffIdCard,
                'entryTime': this.dataForm.entryTime,
                'status': this.dataForm.status,
                'maxFree': this.dataForm.maxFree,
                'maxManualOffer': this.dataForm.maxManualOffer,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.disabledSubmit = false
              }
            })
          }else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
